/*
silverPDF is sponsored by Aleyant Systems (http://www.aleyant.com)

silverPDF is based on PdfSharp (http://www.pdfsharp.net) and iTextSharp (http://itextsharp.sourceforge.net)

Developers: Ai_boy (aka Oleksii Okhrymenko)

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above information and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR SPONSORS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

*/
using System;
using System.Diagnostics;
//using System.Drawing;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System.Resources;
using silverPDF._compatebleLayer;
using System.Windows;

namespace Graphics
{
    /// <summary>
    /// Shows how to draw images.
    /// </summary>
    public class Images : Base
    {
        string jpegSamplePath = "/Graphics;component/images/Z3.jpg";
        string gifSamplePath = "/Graphics;component/images/Test.gif";
        string pngSamplePath = "/Graphics;component/images/Test.png";
        string tiffSamplePath = "/Graphics;component/images/Rose(RGB8).tif";
        string pdfSamplePath = "/Graphics;component/PDFs/SomeLayout.pdf";

        public void DrawPage(PdfPage page)
        {
            XGraphics gfx = XGraphics.FromPdfPage(page);

            DrawTitle(page, gfx, "Images");

            DrawImage(gfx, 1);
            DrawImageScaled(gfx, 2);
            DrawImageRotated(gfx, 3);
            DrawImageSheared(gfx, 4);
            DrawGif(gfx, 5);
            DrawPng(gfx, 6);
            //DrawTiff(gfx, 7);
            //DrawFormXObject(gfx, 8);
        }

        /// <summary>
        /// Draws an image in original size.
        /// </summary>
        void DrawImage(XGraphics gfx, int number)
        {
            BeginBox(gfx, number, "DrawImage (original)");

            XImage image = FromFile(jpegSamplePath);

            // Left position in point
            double x = (250 - image.PixelWidth * 72 / image.HorizontalResolution) / 2;
            gfx.DrawImage(image, x, 0);

            EndBox(gfx);
        }

        /// <summary>
        /// Draws an image scaled.
        /// </summary>
        void DrawImageScaled(XGraphics gfx, int number)
        {
            BeginBox(gfx, number, "DrawImage (scaled)");

            XImage image = FromFile(jpegSamplePath);
            gfx.DrawImage(image, 0, 0, 250, 140);

            EndBox(gfx);
        }

        private XImage FromFile(string path)
        {
            var i = App.GetResourceStream(new Uri(path, UriKind.RelativeOrAbsolute));
            return XImage.FromStream(i.Stream);
        }

        /// <summary>
        /// Draws an image transformed.
        /// </summary>
        void DrawImageRotated(XGraphics gfx, int number)
        {
            BeginBox(gfx, number, "DrawImage (rotated)");

            XImage image = FromFile(jpegSamplePath);

            double dx = 250, dy = 140;

            gfx.TranslateTransform(dx / 2, dy / 2);
            gfx.ScaleTransform(0.7);
            gfx.RotateTransform(-25);
            gfx.TranslateTransform(-dx / 2, -dy / 2);

            XMatrix matrix = XMatrix.Identity;

            double width = image.PixelWidth * 72 / image.HorizontalResolution;
            double height = image.PixelHeight * 72 / image.HorizontalResolution;

            gfx.DrawImage(image, (dx - width) / 2, 0, width, height);

            EndBox(gfx);
        }

        /// <summary>
        /// Draws an image transformed.
        /// </summary>
        void DrawImageSheared(XGraphics gfx, int number)
        {
            BeginBox(gfx, number, "DrawImage (sheared)");

            XImage image = FromFile(jpegSamplePath);

            double dx = 250, dy = 140;

            //XMatrix matrix = gfx.Transform;
            //matrix.TranslatePrepend(dx / 2, dy / 2);
            //matrix.ScalePrepend(-0.7, 0.7);
            //matrix.ShearPrepend(-0.4, -0.3);
            //matrix.TranslatePrepend(-dx / 2, -dy / 2);
            //gfx.Transform = matrix;

            gfx.TranslateTransform(dx / 2, dy / 2);
            gfx.ScaleTransform(-0.7, 0.7);
            gfx.ShearTransform(-0.4, -0.3);
            gfx.TranslateTransform(-dx / 2, -dy / 2);

            double width = image.PixelWidth * 72 / image.HorizontalResolution;
            double height = image.PixelHeight * 72 / image.HorizontalResolution;

            gfx.DrawImage(image, (dx - width) / 2, 0, width, height);

            EndBox(gfx);
        }

        /// <summary>
        /// Draws a GIF image with transparency.
        /// </summary>
        void DrawGif(XGraphics gfx, int number)
        {
            this.backColor = XColors.LightGoldenrodYellow;
            this.borderPen = new XPen(XColor.FromArgb(202, 121, 74), this.borderWidth);
            BeginBox(gfx, number, "DrawImage (GIF)");

            XImage image = FromFile(gifSamplePath);

            double dx = 250, dy = 140;

            double width = image.PixelWidth * 72 / image.HorizontalResolution;
            double height = image.PixelHeight * 72 / image.HorizontalResolution;

            //gfx.DrawImage(image, (dx - width) / 2, (dy - height) / 2, width, height);

            EndBox(gfx);
        }

        /// <summary>
        /// Draws a PNG image with transparency.
        /// </summary>
        void DrawPng(XGraphics gfx, int number)
        {
            BeginBox(gfx, number, "DrawImage (PNG)");

            XImage image = FromFile(pngSamplePath);

            double dx = 250, dy = 140;

            double width = image.PixelWidth * 72 / image.HorizontalResolution;
            double height = image.PixelHeight * 72 / image.HorizontalResolution;

            gfx.DrawImage(image, (dx - width) / 2, (dy - height) / 2, width, height);

            EndBox(gfx);
        }

        /// <summary>
        /// Draws a TIFF image with transparency.
        /// </summary>
        void DrawTiff(XGraphics gfx, int number)
        {
            XColor backColor = this.backColor;
            this.backColor = XColors.LightGoldenrodYellow;
            BeginBox(gfx, number, "DrawImage (TIFF)");

            XImage image = FromFile(tiffSamplePath);

            double dx = 250, dy = 140;

            double width = image.PixelWidth * 72 / image.HorizontalResolution;
            double height = image.PixelHeight * 72 / image.HorizontalResolution;

            gfx.DrawImage(image, (dx - width) / 2, (dy - height) / 2, width, height);

            EndBox(gfx);
            this.backColor = backColor;
        }

        /// <summary>
        /// Draws a form XObject (a page from an external PDF file).
        /// </summary>
        void DrawFormXObject(XGraphics gfx, int number)
        {
            //this.backColor = XColors.LightSalmon;
            BeginBox(gfx, number, "DrawImage (Form XObject)");

            XImage image = FromFile(pdfSamplePath);

            double dx = 250, dy = 140;

            gfx.TranslateTransform(dx / 2, dy / 2);
            gfx.ScaleTransform(0.35);
            gfx.TranslateTransform(-dx / 2, -dy / 2);

            double width = image.PixelWidth * 72 / image.HorizontalResolution;
            double height = image.PixelHeight * 72 / image.HorizontalResolution;

            gfx.DrawImage(image, (dx - width) / 2, (dy - height) / 2, width, height);

            EndBox(gfx);
        }
    }
}
